﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MediaRecommender.Model;
using MediaRecommender.Controller;

namespace MediaRecommender
{
    public partial class Preferences : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            User u = UserCollection.Instance.GetUser((string)Session["Username"]);

            if (!IsPostBack)
            {
                foreach (int pref in Enum.GetValues(typeof(Genre)).Cast<Genre>())
                {
                    PrefList.Items.Add(Enum.GetName(typeof(Genre), pref));
                    PrefList.Items[PrefList.Items.Count - 1].Value = Convert.ToString(pref);
                    if (u.Preferences.Contains(pref))
                    {
                        PrefList.Items[PrefList.Items.Count - 1].Selected = true;
                    }
                }
            }
        }
        protected void CheckBoxList_Changed(object sender, EventArgs e)
        {
            User u = UserCollection.Instance.GetUser((string)Session["Username"]);

            foreach (ListItem listItem in PrefList.Items)
            {
                int currPref = Convert.ToInt32(listItem.Value);
                if (listItem.Selected)
                {
                    if (!u.Preferences.Contains(currPref))
                    {
                        u.Preferences.Add(currPref);
                    }
                }
                else
                {
                    if (u.Preferences.Contains(currPref))
                    {
                        u.Preferences.Remove(currPref);
                    }
                }
            }
        }
    }
}